//
//  StatusViewModel.swift
//  WeiBo
//
//  Created by LTX on 2016/12/1.
//  Copyright © 2016年 LTX. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {

    // MARK:- 定义属性
    var status : StatusModel?
    
    var sourceText :String? //处理后的来源文本
    var createAtText : String? //处理后的时间文本
    
    // MARK:- 用户数据处理
    var verifiedImage : UIImage?
    var vipImage : UIImage?
    var profileURL : NSURL?
    
    var picURLArray : [NSURL] = [NSURL]() //图像URL数组
    
    
    
    // MARK:- 自定义构造函数
    init(status : StatusModel) {
        self.status = status
        
        if let source = status.source , source  != ""{
            //对来源字符串进行处理
            let startIdex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIdex
            
            sourceText = (source as NSString).substring(with: NSRange(location: startIdex, length: length))
            
        }
        
        
        if let created_at = status.created_at  {
            //对时间处理
            createAtText = NSDate.createDateString(createAtStr: created_at)

        }

        //处理认证
        let verifiedType = status.user?.verified_type ?? -1
        
        switch verifiedType {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //处理会员等级
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        
        //处理头像
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = NSURL(string: profileURLString)

        //处理配图
        if let picURLDic = status.pic_urls {
            
            for  picDic in picURLDic {
                
                guard let picURLString  = picDic["thumbnail_pic"] else {
                    continue
                }
                
                //转成URL 保存到数组
                picURLArray.append(NSURL(string: picURLString)!)
                
                
            }
            
        }
        
    }
    
}
